<?php


namespace App\Http\Controllers\Dsc\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class DscController extends Controller
{
    /**
     * 202012 22
     * author 孙凯
     * 大商创吉客云订单下载接口
     */
    public function index()
    {

        $method = $_REQUEST['method'];
        $appkey = $_REQUEST['appkey'];
        $token = $_REQUEST['token'];
        $bizcontent = $_REQUEST['bizcontent'];
        $sign = $_REQUEST['sign'];
        $appSecret = "86426339e6ca4890ae060bc00ce39907";    //appSecret的值通过在菠萝派中申请应用获得

        if (empty($method) || empty($appkey) || empty($token) || empty($bizcontent) || empty($sign)) {
            return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '公共接口缺少必要参数']);
        }

        $unsign = $appSecret . "appkey" . $appkey . "bizcontent" . $bizcontent . "method" . $method . "token" . $token . $appSecret;

        $newsign = md5(strtolower($unsign));
//        echo $newsign;
        if ($sign != $newsign) {
            return response()->json(['code' => '40000', 'message' => 'Logical Error', 'subcode' => 'GSE.VERIFYSIGN_FAILURE', 'submessage' => '签名验证失败']);
        }

        if ($method == "Differ.JH.Business.GetOrder") {
            $request = json_decode($bizcontent, true);
            $PageIndex = $request["PageIndex"] == 0 ? 0 : $request["PageIndex"] - 1;
            $PageSize = $request["PageSize"];
            /**
             * dsc_order_info  ru_id  0 表示自营
             * main_count =0 说明是子订单号
             */
            $where = [
                'ru_id' => 0,
                'main_count' => 0
            ];

            $hanld = DB::table('order_info');
            $hanld = $hanld->where($where);

            if (!empty($request['PlatOrderNo'])) {
                $PlatOrderNo = $request['PlatOrderNo'];
                $hanld->where('order_sn', '=', $PlatOrderNo);
            }

            if (!empty($request['StartTime'])) {
                $hanld->where('add_time', '>=', $request['StartTime'])->get();
            }
            dd($hanld);
            if (!empty($request['EndTime'])) {
                $hanld->where('add_time', '<=', $request['EndTime']);
            }

            $hanld->orderByDesc('add_time');
            $numtotalorder = $hanld->count();
            dd($numtotalorder);
            $res = $hanld->offset($PageIndex)->limit($PageSize)->get()->toArray();


        } elseif ($method == "Differ.JH.Business.Send") {

        } elseif ($method == "Differ.JH.Business.CheckRefundStatus") {

        } elseif ($method == "Differ.JH.Business.DownloadProduct") {

        } elseif ($method == "Differ.JH.Business.SyncStock") {

        } elseif ($method == "Differ.JH.Business.GetRefund") {

        } else {
            return response()->json(['code' => '40000', 'message' => 'System Error', 'subcode' => 'GSE.SYSTEM_ERROR', 'submessage' => '调用接口方法名有误，超出可选范围']);
        }


    }

}